<?python
   from turbogears.i18n import gettext as _
   from mnemoply.utils import date2str_points, time2str
   students = [sig.student for sig in model.StudentInGroup.selectBy(group=obj)]

   def group_name(s):
       if s in students:
           return ""
       return s.cacheGroups

   confirm_text = _('Are you sure you want to delete?')
?>
<span xmlns="http://www.w3.org/1999/xhtml"
      xmlns:py="http://genshi.edgewall.org/">
    <div class="defence_wrap" py:for="defence in model.ProjectDefence.selectBy(group=obj)">
        <?python list_name = 'defence_list_' + str(defence.id) ?>
        <center>
            <a href="/ProjectDefence/${defence.id}/" class="big_link">
                ${date2str_points(defence.date)} (${time2str(defence.startTime)}—${time2str(defence.endTime)})
            </a>
        </center>

        <div class="timetable_container">
            <h4>Student list:</h4>
            <ul id="${list_name}" class="boxy" style="margin-bottom: 1em;">
                <li py:for="ind in defence.getIndividualDefences()"
                    itemID="${ind.id}"
                    style="margin-bottom: 0px; margin-top: 2px;">
                  <?python s=ind.student ?>
                  ${time2str(ind.startTime) + ' ' + s.surname + ' ' + s.forename}
                  (${s.schoolForm})
                  ${group_name(s)}
                  <img src="/static/images/trash.png"
                         class="pseudo_link" alt="Delete" border="0" hspace="4"
                         style="float:right;"
                         onclick="if (confirm('${confirm_text}')) {
                                      parent.location='delete_individual_defence?id_id=${ind.id}'; }" />
                </li>
            </ul>
            <script type="text/javascript" language="javascript">
                 function defenceDragLoad() {
                     ToolMan.junkdrawer().restoreListOrder('${list_name}');
                     try {
                         ToolMan.dragsort().makeListSortable(document.getElementById('${list_name}'), verticalOnly, saveOrder)
                     } catch(e) {
                         return 0;
                     }
                 }

                 function verticalOnly(item) {
                     item.toolManDragGroup.verticalOnly()
                 }

                 function saveOrder(item) {
                     var group = item.toolManDragGroup;
                     var list = group.element.parentNode;
                     var id = list.getAttribute("id");
                     if (id == null) return;
                         group.register('dragend', function() {
                                 ToolMan.cookies().set("list-" + id,
                                     ToolMan.junkdrawer().serializeList(list), 365)
                                 })
                 }

                 addLoadEvent(defenceDragLoad)
            </script>

            <form id="id_${defence.id}" action="calculate" method="POST" style="margin:1em 0 0 0; padding:0pt;">
                <input id="ids_${defence.id}" type="hidden" name="ids" value="" />
                <input id="defence_id_${defence.id}" type="hidden" name="defence_id" value="${defence.id}" />
                <input class="inspector" type="submit" value="${_('Save order')}"
                       onclick="document.getElementById('ids_${defence.id}').value =
                                ToolMan.junkdrawer().serializeList(document.getElementById('${list_name}'));
                                document.getElementById('id_${defence.id}').submit();" />
            </form>
        </div>

        <div class="assistant_container">
            <h4>Assistants:</h4>
            <?python
                assistants = model.ProjectDefenceAssistant.selectBy(defence=defence)
            ?>
            <ul py:if="assistants.count()">
                <li py:for="a in assistants">
                    ${a.assistant.title}
                    <img src="/static/images/trash.png"
                         class="pseudo_link" alt="Delete" border="0" hspace="4"
                         onclick="if (confirm('${confirm_text}')) {
                                      parent.location='delete_defence_assistant?assistant_id=${a.id}'; }" />
                </li>
            </ul>
            <p py:if="not assistants.count()">No avaible assistants for this defence.</p>
            <div style="float:left;">
                ${ET(tab.add_form(value=dict(defence_id=defence.id)))}
            </div>
        </div>
    </div>
</span>

